package com.example.demo.service;

import com.example.demo.enums.BookStatus;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import com.example.demo.dao.BookMapper;
import com.example.demo.model.BookInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    public List<BookInfo> getlist() {
        //获取数据
       // BookDao bookDao = new BookDao();
        List<BookInfo> list = bookMapper.selectall();
        //设置借阅状态
        for (BookInfo book : list) {
            if (book.getStatus() == 1) {
                book.setStatusCN("可借阅");
            } else if(book.getStatus()==2){
                book.setStatusCN("不可借阅");
            }else{
                book.setStatusCN("无效");
            }
        }
        return list;
    }
    public Integer addBook(BookInfo bookInfo){
        return bookMapper.userAddBook(bookInfo);
    }

    public PageResult getBookListPage(PageRequest pageRequest) {
        Integer count=bookMapper.getcount();
        List<BookInfo> list=bookMapper.getBookListPage(pageRequest);
        //设置阅读状态,可设置为枚举，有利于修改代码
//        for (BookInfo book : list) {
//            if (book.getStatus() == "1") {
//                book.setStatus("可借阅");
//            } else if(book.getStatus()=="2") {
//                book.setStatus("不可借阅");
//            }else{
//                book.setStatus("无效");
//            }
//        }
        for(BookInfo bookInfo:list){
            bookInfo.setStatusCN(BookStatus.getBookStatus(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(count,list,pageRequest);
    }

    public BookInfo getistMessage(Integer bookid) {
        BookInfo bookInfo=bookMapper.getBookListmessage(bookid);
        bookInfo.setStatusCN(BookStatus.getBookStatus(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }

    public void updateBook(BookInfo bookInfo) {
       bookMapper.updateBook(bookInfo);
    }

    public Integer delete(Integer bookid) {
        BookInfo bookInfo=new BookInfo();
        bookInfo.setId(bookid);
        bookInfo.setStatus(0);
       return bookMapper.updateBook(bookInfo);
    }

    public void bacthDeleteBook(List<Integer> ids) {
       Integer sum=bookMapper.bacthDeleteBook(ids);
    }
}


